package com.example.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import com.example.demo.dto.DepartmentDTO;
import com.example.demo.dto.PageExtra;
import com.example.demo.request.CreateDepartmentRequest;
import com.example.demo.response.ApiResponse;
import com.example.demo.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/departments")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping
    public ResponseEntity<ApiResponse<List<DepartmentDTO>>> getDepartments(
           @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            HttpServletRequest request) {

        List<DepartmentDTO> departments = departmentService.getDepartments(page, size);
        long total = departmentService.countDepartments();
        PageExtra extra = new PageExtra(page, size, total);

        return ResponseEntity.ok(
                ApiResponse.success(departments, "请求成功", request.getRequestURI(), extra)
        );
    }

    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<DepartmentDTO>> getDepartment(
            @PathVariable Integer id,
            HttpServletRequest request) {

        DepartmentDTO department = departmentService.getDepartmentById(id);
        return ResponseEntity.ok(
                ApiResponse.success(department, "查询成功", request.getRequestURI(), null)
        );
    }


    @PostMapping
    public ResponseEntity<ApiResponse<DepartmentDTO>> createDepartment(
            @Valid @RequestBody CreateDepartmentRequest request,
            HttpServletRequest requestObj) {

        com.example.demo.entity.Department department = departmentService.createDepartment(request);
        DepartmentDTO dto = DepartmentDTO.fromEntity(department);
        return ResponseEntity.status(201).body(
                ApiResponse.created(dto, "部门创建成功", requestObj.getRequestURI())
        );
    }

    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<DepartmentDTO>> updateDepartment(
            @PathVariable Integer id,
            @Valid @RequestBody CreateDepartmentRequest request,
            HttpServletRequest requestObj) {

        departmentService.updateDepartment(id, request);
        DepartmentDTO department = departmentService.getDepartmentById(id);
        return ResponseEntity.ok(
                ApiResponse.success(department, "部门更新成功", requestObj.getRequestURI(), null)
        );
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteDepartment(
            @PathVariable Integer id,
            HttpServletRequest requestObj) {

        departmentService.deleteDepartment(id);
        return ResponseEntity.ok(
                ApiResponse.success(null, "部门删除成功", requestObj.getRequestURI(), null)
        );
    }

    @GetMapping("/search")
    public ResponseEntity<ApiResponse<List<DepartmentDTO>>> searchDepartments(
          @RequestParam String name,
            HttpServletRequest request) {
        
        List<DepartmentDTO> departments = departmentService.searchDepartmentsByName(name);
        return ResponseEntity.ok(
                ApiResponse.success(departments, "搜索成功", request.getRequestURI(), null)
        );
    }
}